/*
 Copyright (c) 2013 OpenSourceRF.com.  All right reserved.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 See the GNU Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/*
The RFduino contains 256KB of usable flash memory organized into 256 1K pages.
An entire 1K pages has to be erased before it can be written too.  The flash
resets the page to all 0xff.  The flash is rated for 10,000 erase/write cycles.

The first 123 pages of flash contain the RFDSTACK, and should never be erased.
Pages 252 thru 255 are reserved for the stack data storage.

Pages 124 through 251 are open and available for use, however, your sketch also
occupies flash memory (starting from Page 124), so it is recommended that you
start using flash pages from 251 downwards.

The flash functions below ensure that reserved pages are not written too, and
that your sketch is not erased or overwritten.

Following is the memory layout of the RFduino:

// flash:
// 00000 - 1EFFF -> RFDSTACK
// 1F000 - 3EFFF -> application and flash space (flash page 124 to 251)
// 3F000 - EFFFF -> stack data storage

// ram:
// 20000000 - 20001FFF -> stack RAM storage
// 20002000 - 20003FFF -> application RAM storage
*/

#ifndef Memory_h
#define Memory_h

#ifdef __cplusplus
extern "C" {
#endif

// divide the address by 1024
#define  PAGE_FROM_ADDRESS(address)  ((uint8_t)((uint32_t)address >> 10))

// multiple the page by 1024
#define  ADDRESS_OF_PAGE(page)  ((uint32_t*)(page << 10))

// generated by RFduino.ld linker script
extern uint32_t _sfixed;
extern uint32_t _efixed;
extern uint32_t _etext;
extern uint32_t _srelocate;
extern uint32_t _erelocate;
extern uint32_t _etextrelocate;
extern uint32_t _sbss;
extern uint32_t _ebss;
extern uint32_t _sstack;
extern uint32_t _estack;
extern uint32_t _end;
extern uint32_t _sflash;
extern uint32_t _eflash;

// return the amount of RAM being used by the sketch
extern int ramUsed(void);

// return the amount of FLASH being used by the sketch
extern int flashUsed(void);

// erase 1K flash page
// inputs:
//   page: must be between 124 and 251
// returns;
//   0 = success
//   1 = reserved page (< 124 or >251)
//   2 = page contains sketch
int flashPageErase( uint8_t page );

// write to a 1K flash page that has been previously erased
// returns:
//   0 = success
//   1 = reserved page (< 124 or >251)
//   2 = page contains sketch
int flashWrite( uint32_t *address, uint32_t value );

// write to a 1K flash page a block cb bytes long from src to dst
int flashWriteBlock( void *dst, const void *src, int cb );

#ifdef __cplusplus
}
#endif

#endif // Memory_h
